package cn.hxzy.service.impl;

import cn.hxzy.domain.Chapter;
import cn.hxzy.domain.Section;
import cn.hxzy.mapper.ChapterMapper;
import cn.hxzy.mapper.SectionMapper;
import cn.hxzy.service.ChapterService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author niuyi
 * @since 2024-06-22
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {


    @Autowired
    private ChapterMapper chapterMapper;

    @Autowired
    private SectionMapper sectionMapper;

    /**
     * 根据课程ID查询该课程下的所有章节
     *
     * @param courseId
     * @return
     */
    @Override
    public Map courseList(Integer courseId) {
        QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        List<Chapter> chapters = chapterMapper.selectList(queryWrapper);
        Map map = new HashMap<>();
        map.put("chapters",chapters);
        chapters.forEach(chapter -> {
            System.out.println(chapter.getChapterId());
            List<Section> section = sectionMapper.getSectionByChapterId(chapter.getChapterId());
            map.put("section",section);
        });
        return map;
    }

    /**
     * 创建章节(某个课程下面的章节)
     *
     * @param chapter
     * @return
     */


    @Override
    public int increaseChapter(Chapter chapter) {
        Chapter chapterByName = chapterMapper.getChapterByName(chapter.getChapterName());
        if (chapterByName == null) {
            int insert = chapterMapper.insert(chapter);
            return insert;
        }
        return 0;
    }

    /**
     * 根据章节ID修改某章节
     *
     * @param chapterId
     * @return
     */

    @Override
    public int updateChapter(Integer chapterId, String chapterName) {
        int i = chapterMapper.updateChapter(chapterId, chapterName);
        return i;
    }




}
